package com.longyue.ms.common.config.mybatis;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.longyue.ms.common.config.property.JdbcProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;


@Configuration
@AutoConfigureAfter(JdbcProperty.class)
public class DruidConfig {
    @Autowired
    private JdbcProperty jdbcProperty;
    @Bean
    public DataSource getDefaultDataSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(jdbcProperty.getUrl());
        dataSource.setUsername(jdbcProperty.getUsername());
        dataSource.setPassword(jdbcProperty.getPassword());
        dataSource.setDriverClassName(jdbcProperty.getDriver());
        setCommonParams(dataSource);
        return dataSource;
    }



    @Bean
    public PlatformTransactionManager platformTransactionManager() throws SQLException {
        DataSource dataSource = getDefaultDataSource();
        PlatformTransactionManager manager = new DataSourceTransactionManager(dataSource);
        return manager;
    }

    public void setCommonParams(DruidDataSource dataSource) throws SQLException {
        dataSource.setKeepAlive(true);
        //配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
        dataSource.setTimeBetweenEvictionRunsMillis(6000);
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnBorrow(false);
        dataSource.setValidationQuery("select 1");
        dataSource.setMaxActive(10000);
        dataSource.setInitialSize(10);
        dataSource.setMinIdle(5);
        dataSource.setFilters("stat");
        dataSource.setTransactionQueryTimeout(10000);
        dataSource.setConnectionErrorRetryAttempts(3);
        dataSource.setTimeBetweenConnectErrorMillis(3000);
        dataSource.setKillWhenSocketReadTimeout(true);
        dataSource.setConnectionErrorRetryAttempts(3);

    }

    /**
     * 配置druid 过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE");
        filterRegistrationBean.addInitParameter("principalSessionName", "USER_SESSION");
        return filterRegistrationBean;
    }

    /**
     * 配置druid servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", "druid");
        reg.addInitParameter("loginPassword", "druid");
        return reg;
    }
}
